package com.sikulifang.bank.simulation.util;

import io.netty.util.HashedWheelTimer;
import io.netty.util.TimerTask;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName AsyncUtil
 * Description AsyncUtil
 *
 * @author Yat
 * Date 2023年6月15日, 0015 13:47
 * version 1.0
 */
public class AsyncUtil {

    private static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();

    private static final HashedWheelTimer WHEEL_TIMER = new HashedWheelTimer(100, TimeUnit.MILLISECONDS, 1024);

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        return new ThreadPoolExecutor(
                CORE_POOL_SIZE, CORE_POOL_SIZE * 2, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1024 * 3), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }

    public static void putIntoQueue(TimerTask command, long delay) {
        WHEEL_TIMER.newTimeout(command, delay, TimeUnit.SECONDS);
    }

}
